<?php

/**
 * 用户列表
 *
 */
function li($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var;

    $view_var   = array();
    $tbl_members   = $_TABLES['members'];
    $conn       = & get_db_conn();

    $where      = 'WHERE 1 ';
    // 关键字
    $key    = trim(@$_GET['key']);
    $type= @(int)$_GET['type'];
    if($type) {
        $where  .= "AND (type = '$type') ";
    }

    if($key != '') $where  .= "AND (username LIKE '%$key%' ) ";

    // 分页处理开始
    $csql = "SELECT COUNT(*) AS rs_count FROM $tbl_members $where";
    $rs   = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕

    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "`{$order_by}` {$order_desc}, ";
    }

    $sql    = "SELECT * FROM $tbl_members
            $where 
            ORDER BY  $str_order_by uid DESC 
            ";
    $rs = $conn->PageExecute($sql,$page_size,$page);
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束

    return $view_var;
}

function li2($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var;

    $view_var   = array();
    $tbl_members   = "yu_member_log";
    $conn       = & get_db_conn();

    $where      = 'WHERE 1 ';
    // 关键字
    $key    = trim(@$_GET['key']);


    if($key != '') $where  .= "AND (username LIKE '%$key%' ) ";


    // 分页处理开始
    $csql = "SELECT COUNT(*) AS rs_count FROM $tbl_members $where";
    $rs   = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕


    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "`{$order_by}` {$order_desc}, ";
    }

    $sql    = "SELECT * FROM $tbl_members
            $where 
            ORDER BY id DESC 
            ";
    $rs = $conn->PageExecute($sql,$page_size,$page);
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束


    return $view_var;
}

function li3($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var;

    $view_var   = array();
    $tbl_members   = "yu_ymb";
    $conn       = & get_db_conn();

    $where      = 'WHERE 1 ';
    // 关键字
    $key    = trim(@$_GET['key']);


    if($key != '') $where  .= "AND (username LIKE '%$key%' ) ";


    // 分页处理开始
    $csql = "SELECT COUNT(*) AS rs_count FROM $tbl_members $where";
    $rs   = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕


    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "`{$order_by}` {$order_desc}, ";
    }

    $sql    = "SELECT * FROM $tbl_members
            $where 
            ORDER BY id DESC 
            ";
    $rs = $conn->PageExecute($sql,$page_size,$page);
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束


    return $view_var;
}

function read($uid = 0) {
    global $_TABLES, $timestamp, $G_big_integer;

    $view_var   = array();
    $tbl_members   = $_TABLES['members'];
    if($uid > 0) {
        $conn   = & get_db_conn();
        $sql    = "SELECT * FROM $tbl_members WHERE uid = $uid LIMIT 0,1 ";
        $view_var   = $conn->GetRow($sql);
        $view_var['attr']=explode(',',$view_var['attr']);
    } else {
        $view_var   = array(
            'uid' => 0,
            'school_aid' => 0,
            'school_name' => '',
            'username' => '',
            'password' => '',
            'gender'   => 0,
            'regdate'=> $timestamp,
            'email' => '',
            'type' => 0,
            'level' => 0
            );
    }
    return $view_var;
}

function request() {
    global $timestamp, $G_big_integer;
    $data = $_POST['members'];

    $school = explode('|',$data['school_info']);

    $data['uid']        = @(int)$data['uid'];
    $data['type']       = @(int)$data['type'];
    $data['level']      = @(int)$data['level'];
    $data['gender']     = @(int)$data['gender'];
    if($data['type']==1||$data['type']==2||$data['type']==5){
        $data['city']=0;
        $data['city1']=0;
        $data['site_id']=0;
        $data['station_id']=0;
		$data['qx']=0;
    }else{
        if($data['qx']==1){
            $data['site_id']=0;
            $data['station_id']=0;
        }elseif($data['qx']==2){
            $data['city']=0;
            $data['city1']=0;
        }
    }
    $attrstr = implode(',',$_POST['attr']);
    $data['attr']	 = $attrstr;//会员属性
    
    $tmp    = explode('-', $data['regdate']);
    $data['regdate'] = mktime(0 ,0, 0,$tmp[1] ,$tmp[2], $tmp[0]);


    $rule   = array(
        'username' => 'i_string|len:1:100', 
        );
    $def_err    = array(
        'username' => '“用户名”必须填写，并且不能超过100个字符！', 
        );
    if($data['can_not_modify_pass'] == 1) {
        $data['password']   = $data['old_password'];
    } else {
        $data['confirmpass'] = md5($data['confirmpass']);
        $data['password']   = md5($data['password']);
    }

    $result = data_validator($data, $rule, $def_err);
    if($result === true) {
        if($data['can_not_modify_pass'] == 0 && $data['confirmpass'] !=$data['password']) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                    "两次输入密码不一致！", 
                    __FILE__,
                    __LINE__);
        }
    } else {
        foreach($result as $v) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                        $v, 
                        __FILE__,
                        __LINE__);
        }
    }

    if($GLOBALS['ERR']->Count > 0)
        return false;
    else {
        /**
         * 接受数据前的处理工作，例如上传等
         *
         */
        global $mod, $G_abs_includes;
        require($G_abs_includes . '/upload.php');
        $up = upload_files($mod);

        $data['img'] = '';
        $data['up_img'] = '';
        if(!$data['delimg'] && $up) {
            $data['up_img']= empty($up['img']) ? '' : $up['img'];
        }
        
        return $data;
    }
}

function add() {
    global $_TABLES, $timestamp;

    $tbl_members = $_TABLES['members'];
    $tbl_attachments = $_TABLES['attachments'];		
    $result = $_POST["members"];
    if($result !== false) {
        $conn   = & get_db_conn();

        // 查看系统中是否有同名广告
        $sql = "SELECT COUNT(*) AS `rs_count` FROM $tbl_members 
                        WHERE `username` = '{$result['username']}' ";
        $rs  = $conn->GetRow($sql);
        if($rs['rs_count'] > 0) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                "当前系统中已经存在用户名为<FONT COLOR=\"red\">{$result['title']}</FONT>的会员，无法完成当前添加操作！", 
                __FILE__,
                __LINE__);
            return false;
        }
		
		if($result['attr']==1){
			$result['display']=1;
		}else{
			$result['display']=0;
		}
        $result['regdate']   = $timestamp;
        if($result['up_img'] == '') {
            $result['img_aid'] = 0;
            $result['img'] = '';
        } else {
            $result['img_aid'] = $result['up_img']['aid'];
            $result['img'] = $result['up_img']['destfile'];
        }
        if($conn->AutoExecute($tbl_members, $result, 'INSERT')) {
            $uid = $conn->Insert_ID();
            // 处理图片
            $update_sql = "UPDATE $tbl_attachments SET item_id = '$uid', upvar = 'img' WHERE aid = '{$result['img_aid']}' ";
            $conn->Execute($update_sql);
            //members_cache();
            return $uid;
        }
    }
    return false;
}

function modify() {
    global $_TABLES, $timestamp;

    $tbl_members = $_TABLES['members'];
    $tbl_attachments = $_TABLES['attachments'];
    $result = $_POST["members"];

    if($result !== false) {
        $conn   = & get_db_conn();


        // 查看系统中是否有同名广告
        $sql = "SELECT COUNT(*) AS `rs_count` FROM $tbl_members 
                        WHERE `username` = '{$result['username']}' 
                          AND uid <> '{$result['uid']}'";
        $rs  = $conn->GetRow($sql);
        if($rs['rs_count'] > 0) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                "当前系统中已经存在用户名为<FONT COLOR=\"red\">{$result['title']}</FONT>的会员，无法完成当前修改操作！", 
                __FILE__,
                __LINE__);
            return false;
        }
	//		print_r($result);
//	exit;
		if($result['attr']==1){
			$result['display']=1;
		}else{
			$result['display']=0;
		}
        // 删除图片
        if(@(int)$result['delimg'] == 1) {
            $result['img_aid'] = 0;
            $result['img'] = '';
            $sql = "SELECT img, img_aid FROM $tbl_members WHERE id = '{$result['id']}'";
            $rs  = $conn->GetRow($sql);
            $conn->Execute("UPDATE $tbl_attachments SET item_id = 0 WHERE item_id = '{$rs['img_aid']}' ");
        } else {
            if($result['up_img'] == '') {
                unset($result['img_aid']);
                unset($result['img']);
            } else {
                $result['img_aid'] = $result['up_img']['aid'];
                $result['img'] = $result['up_img']['destfile'];


                $sql = "SELECT img,img_aid  FROM $tbl_members WHERE id = '{$result['id']}'";

                $rs  = $conn->GetRow($sql);
                $conn->Execute("UPDATE $tbl_attachments SET item_id = 0 WHERE item_id = '{$rs['img_aid']}' ");


                $update_sql = "UPDATE $tbl_attachments SET item_id = '{$result['img_aid']}', upvar = 'img' WHERE aid = '{$result['img_aid']}' ";
                $conn->Execute($update_sql);
            }
        }
        if($conn->AutoExecute($tbl_members, $result,'UPDATE', 'uid=' . $result['uid'] )) {
            //members_cache();
            return $result['uid'];
        }
    }
    return false;
}

function del() {
    global $_TABLES, $mod, $save_flag;
    $tbl_members = $_TABLES['members'];
    $tbl_attachments = $_TABLES['attachments'];

    $deltype    = @(int)$_POST['deltype'];
    if($deltype == $save_flag) {
        // 删除POST传递过来的 item_id 
        // 批量删除
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        // get传递过来的删除 单条删除
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }

    if($str_in != '') {
        $conn   = & get_db_conn();
        // 删除广告图片
        $conn->Execute("UPDATE $tbl_attachments SET item_id = 0 WHERE upmod = '$mod' AND upvar='img' AND item_id IN ($str_in)");
        // 删除广告纪录
        $sql    = "DELETE FROM $tbl_members 
                    WHERE uid IN ({$str_in}) ";
        if($conn->Execute($sql)) {
            //members_cache();
            js_success('删除指定会员成功！');
        }
    }
}

function check() {
    global $_TABLES, $mod, $save_flag;
    $tbl_members = $_TABLES['members'];
    $tbl_attachments = $_TABLES['attachments'];

    $deltype    = @(int)$_POST['deltype'];
    if($deltype == $save_flag) {
        // 审核POST传递过来的 item_id 
        // 批量审核
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        // get传递过来的审核 单条审核
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }

    if($str_in != '') {
        $conn   = & get_db_conn();       
       
        $sql    = "update $tbl_members set display=1 WHERE uid IN ({$str_in}) ";
		
        if($conn->Execute($sql)) {
            //members_cache();
            js_success('审核指定会员成功！');
        }
    }
}

function cz() {
    global $_TABLES, $mod, $save_flag;
    $tbl_members = $_TABLES['members'];

    $uid    = @(int)$_POST['uid'];
    $allfee    = @(int)$_POST['allfee'];
	$stime  = time();
    $conn   = & get_db_conn(); 
    $sql    = "update $tbl_members set balance=balance+$allfee WHERE uid=$uid ";
	if($conn->Execute($sql)) {
		$sq="insert into yu_account_log(userid,type,flow,balance,amount,object_type,object_id,change_date,remark,add_time) values($uid,0,0,$allfee,$allfee,1,0,now(),'管理员为会员充值',$stime)";
		//echo $sq;
//		exit;
		$conn->Execute($sq);
		echo "<script>alert('为会员充值成功！');location='/admin/admin.php?mod=members&action=li';</script>";
		exit;
    }
   
}

function ddel() {
    global $_TABLES, $mod, $save_flag;
    $tbl_members = "yu_member_log";
    $deltype    = @(int)$_POST['deltype'];
    if($deltype == $save_flag) {
        // 删除POST传递过来的 item_id 
        // 批量删除
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        // get传递过来的删除 单条删除
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }

    if($str_in != '') {
        $conn   = & get_db_conn();
        // 删除广告纪录
        $sql    = "DELETE FROM $tbl_members 
                    WHERE id IN ({$str_in}) ";
        if($conn->Execute($sql)) {
            //members_cache();
            js_success('删除指定日志成功！');
        }
    }
}

// 生成缓存
/*
function members_cache() {
    global $_TABLES, $cfg_cache, $members_class, $G_abs_upload, $G_web_upload;

    $tbl_members     = $_TABLES['members'];
    $conn   = & get_db_conn();

    $membersvar= array();
    $sql = "SELECT * FROM $tbl_members WHERE display = 1 ORDER BY uid ASC ";
    $rs = $conn->Execute($sql);
    if($rs->EOF) {
    } else {
        while (!$rs->EOF) {
            $membersvar[]  = array(
                'uid'           => $rs->fields['uid'],
                'school_aid'    => $rs->fields['school_aid'],
                'school_name'   => $rs->fields['school_name'],
                'username'      => $rs->fields['username'],
                'password'      => $rs->fields['password'],
                'gender'        => $rs->fields['gender'],
                'regdate'       => $rs->fields['regdate'],
                'email'         => $rs->fields['email'],
                'type'          => $rs->fields['type'],
                'level'         => $rs->fields['level'],
                );
            $rs->MoveNext();
        }
    }

    // 写缓存
    $file_name  = $cfg_cache['cache_abspath'] . '/' . $cfg_cache['//members_cache_filename'];
    $str_var    = "<"."?php\r\n" . 
            "\$" . $cfg_cache['//members_cache_var'] . " = " . var_export_str($membersvar) . ";\r\n".
            "?".">";
    write_to_file($file_name, $str_var);
}
*/
?>